package com.ruoyi.project.rate.service;

import org.springframework.stereotype.Service;

import com.ruoyi.framework.web.service.CrudService;
import com.ruoyi.project.rate.domain.Sup;
import com.ruoyi.project.rate.mapper.SupMapper;

@Service
public class SupService extends CrudService<SupMapper, Sup> {
//	@Autowired
//	private SupMapper supMapper;
//	@Autowired
//	private SupTrackService supTrackService;
//	@Autowired
//	private IDictDataService dictDataService;

	/**
	@Transactional(readOnly = false)
	public int submitToCheck(Sup sup) {
		sup.setStatus(Constants.KH_STATUS_KHZ);
		
		int i = this.supMapper.updateStatus(sup);
		
		this.addTrack(sup, "提交考核组审批", "提交考核组审批成功");
		return i;
	}
	**/
	
	/**
	@Transactional(readOnly = false)
	public int batchSubmitToCheck(Sup sup, String ids) {
		int checkedCount = 0;
		String[] idArray = ids.split(",");
		for (String id : idArray) {
			Sup _sup = this.get(id);
			if (_sup == null) {
				continue;
			}
			
			if (!StringUtils.equalsAny(_sup.getStatus(),
					Constants.KH_STATUS_DRAFT,
					Constants.KH_STATUS_REJECTED)) {
				continue;
			}
			
			this.submitToCheck(_sup);
			checkedCount++;
		}
		
		return checkedCount;
	}
	**/
	
	/**
	@Transactional(readOnly = false)
	public void submitCheck(Sup sup) {
		String pageFlag = sup.getPageFlag();
		
		String operation = dictDataService.selectDictLabel(
				Constants.KH_STATUS, pageFlag);
		if (StringUtils.isBlank(operation)) {
			throw new RuntimeException("error operation[submitCheck]");
		}
		
		String next = null;
		if (Constants.KH_STATUS_KHZ.equals(pageFlag)) {
			next = Constants.KH_STATUS_FGLD;
		} else if (Constants.KH_STATUS_FGLD.equals(pageFlag)) {
			next = Constants.KH_STATUS_HZ;
		} else if (Constants.KH_STATUS_HZ.equals(pageFlag)) {
			next = Constants.KH_STATUS_APPROVED;
		}
		if (StringUtils.isBlank(next)) {
			throw new RuntimeException("error next status[submitCheck]");
		}
		
		if (Constants.KH_STATUS_APPROVED.equals(sup.getCheckResult())) {
			sup.setStatus(next);
		} else {
			sup.setStatus(Constants.KH_STATUS_REJECTED);
		}
		
		sup.setCheckTime(new Date());
		this.supMapper.submitCheck(sup);
		
		if (Constants.PAGE_FLAG_KHZ.equals(pageFlag)) {
			this.supMapper.updateFgldChecker(sup);
		}
		
		// 记录轨迹
		String result = Constants.KH_STATUS_APPROVED.equals(sup.getCheckResult()) ? "审核通过" : "审核不通过";
		this.addTrack(sup, operation, result + "，审核意见：" + sup.getCheckDesc());
	}
	**/
	
	/**
	@Transactional(readOnly = false)
	public int batchSubmitCheck(Sup sup, String ids) {
		int checkedCount = 0;
		String[] idArray = ids.split(",");
		for (String id : idArray) {
			Sup _sup = this.get(id);
			if (_sup == null) {
				continue;
			}
			
			if (!StringUtils.equals(_sup.getStatus(), sup.getPageFlag())) {
				continue;
			}
			
			_sup.setPageFlag(sup.getPageFlag());
			_sup.setFgldChecker(sup.getFgldChecker());
			_sup.setCheckResult(sup.getCheckResult());
			if (Constants.KH_STATUS_APPROVED.equals(_sup.getCheckResult())) {
				_sup.setCheckDesc("通过");
			} else {
				_sup.setCheckDesc("拒绝");
			}
			
			this.submitCheck(_sup);
			checkedCount++;
		}
		
		return checkedCount;
	}
	**/

	/**
	@Transactional(readOnly = false)
	public void cancelCheck(Sup sup) {
		String pageFlag = sup.getPageFlag();
		String operation = dictDataService.selectDictLabel(Constants.KH_STATUS, pageFlag);
		if (StringUtils.isBlank(operation)) {
			throw new RuntimeException("error operation[cancelCheck]");
		}
		operation += "-撤销";

		Sup exists = this.get(sup);
		if (exists == null) {
			throw new RuntimeException("查询不到记录.");
		}
		String currentStatus = exists.getStatus();
		boolean canCancelCheck = false;

		// 校验是否可撤回审核
		if (Constants.KH_STATUS_HZ.equals(pageFlag)
				&& Constants.KH_STATUS_APPROVED.equals(currentStatus)) {
			canCancelCheck = true;
		} else if (Constants.KH_STATUS_FGLD.equals(pageFlag)
				&& Constants.KH_STATUS_HZ.equals(currentStatus)) {
			canCancelCheck = true;
		} else if (Constants.KH_STATUS_KHZ.equals(pageFlag)
				&& Constants.KH_STATUS_FGLD.equals(currentStatus)) {
			canCancelCheck = true;
		}

		if (!canCancelCheck) {
			throw new RuntimeException("当前状态不允许撤回审核.");
		}

		sup.setStatus(pageFlag);
		this.supMapper.updateStatus(sup);

		// 记录轨迹
		this.addTrack(sup, operation, null);
	}
	**/
	
	/**
     * 撤回报表
     */
	/**
    @Transactional(readOnly = false)
    public void cancel(Sup sup) {
    	String operation = null;
    	String currentStatus = sup.getStatus();
    	boolean canCancelRec = false;
    	
		if (Constants.KH_STATUS_KHZ.equals(currentStatus)) {
			canCancelRec = true;
			operation = "撤回";
    		sup.setStatus(Constants.KH_STATUS_DRAFT);
		}
    	
    	if (!canCancelRec) {
    		throw new RuntimeException("不允许当前操作！");
    	}
    	
    	this.supMapper.updateStatus(sup); // 更新状态
    	
    	// 记录轨迹
    	this.addTrack(sup, operation, "撤回成功");
    }
    **/
	
	/**
    @Transactional(readOnly = false)
	public int batchCancel(Sup sup, String ids) {
		int checkedCount = 0;
		String[] idArray = ids.split(",");
		for (String id : idArray) {
			Sup _sup = this.get(id);
			if (_sup == null) {
				continue;
			}
			
			if (!StringUtils.equals(_sup.getStatus(), Constants.KH_STATUS_KHZ)) {
				continue;
			}
			
			this.cancel(_sup);
			checkedCount++;
		}
		
		return checkedCount;
	}
	**/
    
	/**
	private void addTrack(Sup sup, String operation, String remark) {
		SupTrack track = new SupTrack();
		track.setSup(sup);
		track.setOperation(operation);
		track.setRemark(remark);
		track.setCurrentStatus(sup.getStatus());
		this.supTrackService.save(track);
	}
	**/
	
	/**
    @Transactional(readOnly = false)
    public int updateStatus(Sup sup) {
        return this.supMapper.updateStatus(sup);
    }
    **/
}
